package android.FiltrosJava;

import android.graphics.Bitmap;

public class GrayscaleFilter extends Filtro{

	public int delimitaCor(int c){
		if (c < 0)
			return 0;
		if (c > 255)
			return 255;
		return c;
	}

	public Bitmap applyFilter(Bitmap bmp) {
		
	int pixels;
		int height = bmp.getHeight();
		int width = bmp.getWidth();
		System.out.println("width: "+width);
		System.out.println("height: "+height);
		/*AlertDialog ad= new AlertDialog.Builder(this).create();//  new AlertDialog.Builder(this).create();
		ad.setCancelable(false);
		ad.setMessage("wid: "+width+"he: "+height);
		ad.show();*/
//		pixels = new int[height * width];
		//bmp.getPixels(pixels, 0, width, 0, 0, width, height);// ofset(2o param)

		int[] vetPix = new int[4];
		int gray;
		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {
				pixels= bmp.getPixel(x, y);
				vetPix[3] = (pixels >> 24);
				vetPix[2] = ((pixels & 0x00FF0000) >> 16);
				vetPix[1] = ((pixels & 0x0000FF00) >> 8);
				vetPix[0] = (pixels & 0x000000FF);
				
				//vetPix = computeFilter(x, y, vetPix);
			 gray = (vetPix[0] * 77 + vetPix[1] * 151 + vetPix[2] * 28) >> 8;// NTSC luma

				//gray = (imageData[0] * 77 + imageData[1] * 151 + imageData[2] * 28) >> 8;// NTSC luma

				// pixels[c]= rgb2Int(vetPix);
				pixels= ((vetPix[3] & 0xFF) << 24)
						| ((gray & 0xFF) << 16)
						| ((gray & 0xFF) << 8) | (gray & 0xFF);
					bmp.setPixel(x, y, pixels);
				}
				//c++;
			}
	
/*
		Bitmap novo = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
		// novo.setPixels(pixels, offset, stride, x, y, width, height)
		novo.setPixels(pixels, 0, width, 0, 0, width, height);
		if (bmp != null) {
			bmp.recycle();
		}*/
		//pixels = null;
		return bmp;
	}

	@Override
	public int[] applyFilter(int[] pixels) {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * 
	 * public BufferedImage applyFilter(BufferedImage src){ int width =
	 * src.getWidth(); int height = src.getHeight();
	 * 
	 * WritableRaster raster_src = src.getRaster(); int numBands =
	 * raster_src.getNumBands(); int[] imageData = new int[numBands];
	 * 
	 * for ( int y = 0; y < height; y++ ) { for ( int x = 0; x < width; x++ ) {
	 * raster_src.getPixel(x,y,imageData); imageData = computeFilter(x, y,
	 * imageData); raster_src.setPixel(x,y,imageData); } }
	 * 
	 * return src; }
	 */

	//public abstract int[] computeFilter(int px, int py, int[] imageData);

}
